/* -*- c++ -*- */
/* 
 * Copyright 2013 <+YOU OR YOUR COMPANY+>.
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <algorithm>
#include <queue>
#include <gr_io_signature.h>
#include "protocol_sink_impl.h"

namespace gr {
  namespace blocks {

    protocol_sink::sptr
    protocol_sink::make(
	    const std::vector<int> &key_seq,
        int snr_estimation_length,
        int num_errs_allowed
    )
    {
      return gnuradio::get_initial_sptr (new protocol_sink_impl(
        key_seq, snr_estimation_length, num_errs_allowed));
    }

    /*
     * The private constructor
     */
    protocol_sink_impl::protocol_sink_impl(
	    const std::vector<int> &key_seq,
        int snr_estimation_length,
        int num_errs_allowed
    )
      : gr_sync_block("protocol_sink",
		      gr_make_io_signature(1,1, sizeof (char)),
		      gr_make_io_signature(1,1, sizeof (char))),
      d_state(protocol_phases::KEY_SEARCH),
      d_key_seq(key_seq),
      d_key_length(key_seq.size()),
      d_key_search(key_seq.size(), 0),
      d_snr_estimation_length(snr_estimation_length),
      d_symbol_counter(0),
      d_num_errs_allowed(num_errs_allowed)
    {
      std::reverse(d_key_seq.begin(), d_key_seq.end());
    }

    /*
     * Our virtual destructor.
     */
    protocol_sink_impl::~protocol_sink_impl()
    {
    }

    int
    protocol_sink_impl::work(int noutput_items,
			  gr_vector_const_void_star &input_items,
			  gr_vector_void_star &output_items)
    {
        const char *in = (const char *) input_items[0];
        char *out = (char *) output_items[0];

        int i, num_errs;

        switch(d_state) {
        case protocol_phases::KEY_SEARCH:
            // add received symbol to search window
            d_key_search.insert(d_key_search.begin(), in[i]);
            d_key_search.pop_back();
            // compare search window with key
            if(d_key_seq == d_key_search){
                num_errs = 0;
            }else{
                num_errs = 0;
                for(i=0; i<d_key_length; i++){
                    if (d_key_seq[i]!=d_key_search[i]){
                        num_errs += 1;
                    }
                }
            }
 #if 0
            printf("Protocol Sink: search key, num_errs %d\n", num_errs);
            printf("Protocol Sink:");
            for (i=0; i<d_key_search.size(); i++){
                printf("%d ", d_key_search[i]);
            }
            printf("\n");
#endif

            // if ok go to next state
            if (num_errs <= d_num_errs_allowed){
                //printf("Protocol Sink: Found key: on %d symbol\n", d_symbol_counter);

                d_state = protocol_phases::SNR_ESTIMATION;
                d_symbol_counter = 0;
            }

            d_symbol_counter ++;
            consume_each(1);
            produce(0, 0);
            break;

        case protocol_phases::SNR_ESTIMATION:
            consume_each(1);
            produce(0, 0);
            d_symbol_counter ++;

            //printf("Sink: %d, On SNR estimation Receiving symbol %d\n", d_symbol_counter, in[i]);
            if (d_symbol_counter >= d_snr_estimation_length){
                //printf("Sink: %d, finish SNR estimation\n", d_symbol_counter);
                d_state = protocol_phases::RECEIVE_DATA;
            }
            break;

        case protocol_phases::RECEIVE_DATA:
            for(i=0; i<noutput_items; i++){
                out[i] = in[i];
            }
            //printf("Sink: received %d items\n", noutput_items);
            consume_each(noutput_items);
            produce(0, noutput_items);
        }

        return WORK_CALLED_PRODUCE;
    }

  } /* namespace blocks */
} /* namespace gr */

